//
//  DMFacebookTypes.m
//  DualMediationSDK
//
//  Facebook相关类型定义和数据模型实现
//  Created by YeJary on 2025/1/15.
//

#import "DMFacebookTypes.h"

NS_ASSUME_NONNULL_BEGIN

#pragma mark - 错误域定义

NSString * const DMFacebookErrorDomain = @"com.dualmediationsdk.facebook";

#pragma mark - DMFacebookUserInfo实现

@implementation DMFacebookUserInfo

+ (instancetype)userInfoWithDictionary:(NSDictionary *)dictionary {
    DMFacebookUserInfo *userInfo = [[DMFacebookUserInfo alloc] init];
    
    userInfo.userId = dictionary[@"id"] ?: dictionary[@"userId"] ?: @"";
    userInfo.name = dictionary[@"name"] ?: dictionary[@"userName"] ?: @"";
    userInfo.email = dictionary[@"email"];
    
    // 处理头像URL
    if (dictionary[@"picture"]) {
        NSDictionary *picture = dictionary[@"picture"];
        if ([picture isKindOfClass:[NSDictionary class]]) {
            NSDictionary *data = picture[@"data"];
            if ([data isKindOfClass:[NSDictionary class]]) {
                userInfo.pictureURL = data[@"url"];
            }
        }
    } else if (dictionary[@"pictureURL"]) {
        userInfo.pictureURL = dictionary[@"pictureURL"];
    }
    
    userInfo.loginTime = [NSDate date];
    
    return userInfo;
}

- (NSDictionary *)toDictionary {
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    
    if (self.userId) {
        dict[@"userId"] = self.userId;
    }
    
    if (self.name) {
        dict[@"name"] = self.name;
    }
    
    if (self.email) {
        dict[@"email"] = self.email;
    }
    
    if (self.pictureURL) {
        dict[@"pictureURL"] = self.pictureURL;
    }
    
    if (self.loginTime) {
        dict[@"loginTime"] = @([self.loginTime timeIntervalSince1970]);
    }
    
    return [dict copy];
}

- (NSString *)description {
    return [NSString stringWithFormat:@"<DMFacebookUserInfo: userId=%@, name=%@, email=%@>", 
            self.userId, self.name, self.email];
}

@end

#pragma mark - DMFacebookShareResult实现

@implementation DMFacebookShareResult

+ (instancetype)resultWithSuccess:(BOOL)success message:(NSString *)message {
    DMFacebookShareResult *result = [[DMFacebookShareResult alloc] init];
    result.success = success;
    result.message = message ?: (success ? @"分享成功" : @"分享失败");
    result.shareTime = [NSDate date];
    
    return result;
}

- (instancetype)initWithSuccess:(BOOL)success error:(nullable NSError *)error {
    self = [super init];
    if (self) {
        self.success = success;
        self.message = error ? error.localizedDescription : (success ? @"分享成功" : @"分享失败");
        self.shareTime = [NSDate date];
    }
    return self;
}

- (NSString *)description {
    return [NSString stringWithFormat:@"<DMFacebookShareResult: success=%@, message=%@, shareId=%@>", 
            @(self.success), self.message, self.shareId];
}

@end

#pragma mark - 错误创建函数

NSError *DMFacebookError(DMFacebookErrorCode code, NSString *message) {
    NSString *errorMessage = message;
    
    // 如果没有提供消息，使用默认消息
    if (!errorMessage) {
        switch (code) {
            case DMFacebookErrorCodeUnknown:
                errorMessage = @"未知错误";
                break;
            case DMFacebookErrorCodeNotInitialized:
                errorMessage = @"Facebook SDK未初始化";
                break;
            case DMFacebookErrorCodeLoginCancelled:
                errorMessage = @"用户取消登录";
                break;
            case DMFacebookErrorCodeLoginFailed:
                errorMessage = @"Facebook登录失败";
                break;
            case DMFacebookErrorCodeShareFailed:
                errorMessage = @"Facebook分享失败";
                break;
            case DMFacebookErrorCodeUserInfoFailed:
                errorMessage = @"获取用户信息失败";
                break;
            case DMFacebookErrorCodeNetworkError:
                errorMessage = @"网络连接错误";
                break;
            case DMFacebookErrorCodePermissionDenied:
                errorMessage = @"权限被拒绝";
                break;
            case DMFacebookErrorCodeNoAccessToken:
                errorMessage = @"无有效访问令牌";
                break;
            default:
                errorMessage = @"Facebook操作失败";
                break;
        }
    }
    
    NSDictionary *userInfo = @{
        NSLocalizedDescriptionKey: errorMessage,
        NSLocalizedFailureReasonErrorKey: errorMessage
    };
    
    return [NSError errorWithDomain:DMFacebookErrorDomain code:code userInfo:userInfo];
}

#pragma mark - DMLoginResponse实现

@implementation DMLoginResponse

+ (instancetype)successResponseWithUserInfo:(NSDictionary *)userInfo accessToken:(NSString *)accessToken {
    DMLoginResponse *response = [[DMLoginResponse alloc] init];
    response.success = YES;
    response.message = @"登录成功";
    response.userInfo = userInfo;
    response.accessToken = accessToken;
    response.loginTime = [NSDate date];
    
    return response;
}

+ (instancetype)failureResponseWithError:(NSError *)error {
    DMLoginResponse *response = [[DMLoginResponse alloc] init];
    response.success = NO;
    response.message = error.localizedDescription ?: @"登录失败";
    response.error = error;
    response.loginTime = [NSDate date];
    
    return response;
}

- (instancetype)init {
    self = [super init];
    if (self) {
        _success = NO;
        _message = @"";
        _loginTime = [NSDate date];
    }
    return self;
}

- (NSString *)description {
    return [NSString stringWithFormat:@"<DMLoginResponse: success=%@, message=%@, userInfo=%@>", 
            @(self.success), self.message, self.userInfo];
}

@end

NS_ASSUME_NONNULL_END
